package com.app.xinfadi.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.app.xinfadi.util.SharedPreferencesUtil;
import com.app.xinfadi.widget.dialog.CustomProgressDialog;
import com.app.xinfadi.widget.gson.GsonRequest;


public abstract class BaseFragment extends Fragment implements OnClickListener {
    protected Context context;
    private CustomProgressDialog progressDialog;
    private MyApplication myApplication;

    private static String TAG = "BaseFragment";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this.getActivity();
        setHasOptionsMenu(true);
        myApplication = MyApplication.getInstance();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onClick(View v) {
    }

    public <T> void addToRequestQueue(GsonRequest<T> request, String tag) {
        MyApplication.getInstance().addToRequestQueue(request, tag);
    }

    public <T> void addToRequestQueue(GsonRequest<T> request) {
        MyApplication.getInstance().addToRequestQueue(request);
    }

    public void cancelPendingRequests(Object tag) {
        MyApplication.getInstance().cancelPendingRequests(tag);
    }

    /**
     * 是否登陆
     *
     * @return
     */
    public boolean isLogin() {
        if (SharedPreferencesUtil.getInstance().getUserState() == 0) {
            return false;
        } else {
            return true;
        }
    }

    public void showProgressDialog() {
        closeSoftKeyBoard(getActivity());
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
        progressDialog = createProgressDialog();
        progressDialog.setMessage("加载中...");
        progressDialog.show();
    }


    /**
     * 显示 旋转progress
     *
     * @param message 文字内容
     */
    public void showProgressDialog(String message) {
        closeSoftKeyBoard((Activity) context);
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
        WindowManager.LayoutParams lp = ((BaseActivity) context).getWindow().getAttributes();
        lp.alpha = .7f;
        ((BaseActivity) context).getWindow().setAttributes(lp);
        progressDialog = createProgressDialog();
        progressDialog.setMessage(message);
        progressDialog.show();
    }


    public CustomProgressDialog createProgressDialog() {
        dismissProgressDialog();
        progressDialog = CustomProgressDialog.createDialog(getActivity());
        progressDialog.setMessage("加载中...");
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();
        // progressDialog.setContentView(R.layout.dialog_loading_process);
        return progressDialog;
    }

    /**
     * 隐藏正在加载的进度条
     */
    public void dismissProgressDialog() {
        try {
            if (progressDialog != null) {
                WindowManager.LayoutParams lp = ((BaseActivity) context).getWindow().getAttributes();
                lp.alpha = 1.0f;
                ((BaseActivity) context).getWindow().setAttributes(lp);
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                progressDialog = null;
            }
        } catch (Exception e) {
        }
    }

    /**
     * 关闭系统软键盘
     */
    public static void closeSoftKeyBoard(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null && activity.getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 按照其图片比例 缩放控件
     *
     * @param drawable
     * @param view
     */
    public void equalProportionDrawable(Drawable drawable, View view) {
        int imageHeight = drawable.getIntrinsicHeight()
                * (myApplication.screenWidth - view.getPaddingLeft() - view.getPaddingRight())
                / drawable.getIntrinsicWidth();
        LayoutParams drawableLayoutParams = view.getLayoutParams();
        drawableLayoutParams.height = imageHeight;
        drawableLayoutParams.width = MyApplication.getInstance().screenWidth;
        view.setLayoutParams(drawableLayoutParams);
    }

    /**
     * dip转换px
     */
    public int dip2px(int dip) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    /**
     * pxz转换dip
     */
    public int px2dip(int px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }
}
